package org.tnaggs.project.manager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.tnaggs.project.constants.Roles;
import org.tnaggs.project.model.Contact;
import org.tnaggs.project.model.Game;
import org.tnaggs.project.model.League;
import org.tnaggs.project.model.Team;
import org.tnaggs.project.repository.ContactRepository;
import org.tnaggs.project.repository.ConversionRepository;
import org.tnaggs.project.repository.GameRepository;
import org.tnaggs.project.repository.LeagueRepository;
import org.tnaggs.project.repository.TeamRepository;

public class LeagueManager {

	private LeagueRepository leagueRepo;
	private TeamRepository teamRepo;
	
	@Autowired
	private GameRepository gameRepo;
	
	@Autowired
	private ContactRepository contactRepo;
	
	@Autowired
	private ConversionRepository conversionRepo;
	
	
	public LeagueManager(LeagueRepository leagueRepo, TeamRepository teamR) {
		this.setLeagueRepo(leagueRepo);
		setTeamRepo(teamR);
	}

	public LeagueRepository getLeagueRepo() {
		return leagueRepo;
	}

	public void setLeagueRepo(LeagueRepository leagueRepo) {
		this.leagueRepo = leagueRepo;
	}
	
	public List<League> getAllLeagues() {
		return leagueRepo.findAll();
	}

	public void createLeague(League league) {
		leagueRepo.insert(league);
	}

	public void createTeam(Team team) {
		teamRepo.insert(team);
	}
	
	public List<Team> getTeams(String leagueName) { 
		League league = leagueRepo.findByName(leagueName);
		List<Team> teams = teamRepo.findAllByLeague(league.getLeagueId());
		return teams;
	}

	public TeamRepository getTeamRepo() {
		return teamRepo;
	}

	public void setTeamRepo(TeamRepository teamRepo) {
		this.teamRepo = teamRepo;
	}

	public List<Contact> getAllAdmins() {
		ArrayList<Contact> results = new ArrayList<Contact>();
		List<Contact> contacts = contactRepo.findAll();
		for(Contact contact : contacts) {
			if(conversionRepo.getRole(contact.getRoleID()).equalsIgnoreCase(Roles.ADMIN)) {
				results.add(contact);
			}
		}
        return results;
	}
	
	public List<Contact> getAllCoaches() {
		ArrayList<Contact> results = new ArrayList<Contact>();
		List<Contact> contacts = contactRepo.findAll();
		for(Contact contact : contacts) {
			if(conversionRepo.getRole(contact.getRoleID()).equalsIgnoreCase(Roles.COACH)) {
				results.add(contact);
			}
		}
        return results;
	}

	public List<Game> getGames(String leagueName) {
		ArrayList<Game> realResults = new ArrayList<Game>();
		HashMap<String,Game> results = new HashMap<String,Game>();
		
		for(Team team : getTeams(leagueName)) {
			for(Game game : gameRepo.findByTeamID(team.getTeamID())) {
		      String gameID = Integer.toString(game.getGameID());
			  if(results.get(gameID) == null) {
				  results.put(gameID, game);
			  }
			}
		}
		for(Game game : results.values()) {
			realResults.add(game);
		}
		return realResults;
	}
	
	public ContactRepository getContactRepo() {
		return contactRepo;
	}

	public void setContactRepo(ContactRepository contactRepo) {
		this.contactRepo = contactRepo;
	}
	
	
	
	
}
